import random
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation

plt.style.use('fivethirtyeight')

x_vals = []
y_vals = []
index = count() # 生成一个无限递增的迭代器 0,1,2,3...

fig, ax = plt.subplots()

def animate(i):
    x_vals.append(next(index))
    y_vals.append(random.randint(0, 5))
    ax.clear()
    ax.plot(x_vals, y_vals)

ani = FuncAnimation(fig, animate, frames=10, interval=1000)
"""
frames 帧数
interval 每帧间隔时间
"""

plt.show()